<?php

/*
 * CopyRight  : (C)2012-2099 HaoTeam Inc.
 * Document   : Article.php
 * Created on : 2022-12-22 11:02:33
 * Author     : Tiger <1192851302@qq.com>
 * Description: This is NOT a freeware, use is subject to license terms.
 *              这即使是一个免费软件,使用时也请遵守许可证条款,得到当时人书面许可.
 *              未经书面许可,不得翻版,翻版必究;版权归属 HaoTeam Inc;
 */

namespace app\models;

use think\Model;

/**
 * 
 * Class Article
 * Tiger <1192851302@qq.com> 2022-12-22 11:02:33
 */
class Article extends Model {

    /**
     * 
     * @author Tiger <1192851302@qq.com>
     * @return type
     */
    public static function validWhere() {
        return self::where('is_del', 0);
    }

    /**
     * 获取数据列表-分页
     * @author Tiger <1192851302@qq.com>
     * @param array $where
     * @return array
     */
    public static function getList($where) {
        $model = self::alias('a')->join('article_category category', 'category.id= a.category_id');
        if (!empty($where['search'])) {
            $model->where('a.title|category.name', 'LIKE', '%' . $where['search'] . '%');
        }
        unset($where['search']);
        $model->where($where)->where('a.is_del', 0)->field([
            'a.*',
            'category.name as typeName'
        ])->order('time desc');
        $models = $model->paginate(input('limit', 10))->each(function($item) {
            $item->message = preg_replace('/<[^>]*>/', '', $item->message);
        });
        return [
            'code' => 0,
            'items' => $models->items(),
            'total' => $models->total()
        ];
    }

    /**
     * 获取数据
     * @author Tiger <1192851302@qq.com>
     * @return array
     */
    public static function getInfo($id) {
        $model = self::alias('a')->join('article_category category', 'category.id= a.category_id');
        $model->where('a.id', $id)->field([
            'a.*',
            'category.name as typeName'
        ]);
        return $model->find();
    }

}
